package riven.club.common.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import java.time.LocalDateTime;

/**
 * 数据库实体对象超类
 *
 * @author 陈浩
 * @date 2021-10-13 16:26
 */
//(1) 该注解标注在类上，用来标识父类；
//(2) 该注解标识的类不能映射到数据库，被标识的类的属性必须通过子类来映射；
//(3) 该注解标识了类之后，不能再有@Entity和@Table注解标识该类
@MappedSuperclass
//(1) 该注解用于监听实体类，在save、update之后的状态
//(2) 使用了@EntityListeners(AuditingEntityListener.class)之后，记得在Application启动类上加@EnableJpaAuditing，不然@CreateDate，@LastModifiedBy不生效
@EntityListeners(AuditingEntityListener.class)
@Data
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })
public abstract class AbstractEntity {

    @CreatedDate
//    @Column(nullable = false)
    LocalDateTime created;
    @LastModifiedDate
//    @Column(nullable = false)
    LocalDateTime updated;
    @Column(nullable = false)
    protected Boolean deleted = Boolean.FALSE;
}
